package com.lwj.service.configuration;

import com.lwj.dto.configuration.DepartmentRoleMenuDto;
import com.lwj.mapper.configuration.DepartmentRoleMenuMapper;
import com.lwj.model.configuration.DepartmentRoleMenu;
import com.lwj.service.DozerService;
import com.lwj.utils.CollectionTool;
import com.lwj.utils.CustomReflectUtil;
import com.lwj.vo.configuration.DepartmentRoleMenuVo;
import com.lwj.vo.system.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class DepartmentRoleMenuServiceImpl implements DepartmentRoleMenuService{
    @Autowired
    private DozerService dozerService;

    @Autowired
    private DepartmentRoleMenuMapper departmentRoleMenuMapper;

    @Override
    public int deleteByPrimaryKey(String departmentRoleMenuId) {
        return departmentRoleMenuMapper.deleteByPrimaryKey(departmentRoleMenuId);
    }

    @Override
    public int insert(DepartmentRoleMenu record) {
        return departmentRoleMenuMapper.insert(record);
    }

    @Override
    public int insertSelective(DepartmentRoleMenu record) {
        return departmentRoleMenuMapper.insertSelective(record);
    }

    @Override
    public DepartmentRoleMenu selectByPrimaryKey(String departmentRoleMenuId) {
        return departmentRoleMenuMapper.selectByPrimaryKey(departmentRoleMenuId);
    }

    @Override
    public int updateByPrimaryKeySelective(DepartmentRoleMenu record) {
        return departmentRoleMenuMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(DepartmentRoleMenu record) {
        return departmentRoleMenuMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<DepartmentRoleMenuVo> menuTreeByRoleId(String departmentRoleId) {
        List<DepartmentRoleMenuVo> list= departmentRoleMenuMapper.menuTreeByRoleId(departmentRoleId);
        return CollectionTool.buildTree2(list);
    }

    @Override
    public void savePermission(DepartmentRoleMenuDto dto) {
        List<String> current = dto.getCurrent();
        List<String> next = dto.getNext();
        List<DepartmentRoleMenuVo> departmentRoleMenuVos = departmentRoleMenuMapper.menuTreeByRoleId(dto.getDepartmentRoleId());
        for (int i = 0; i < departmentRoleMenuVos.size(); i++) {
            DepartmentRoleMenuVo item = departmentRoleMenuVos.get(i);
            String currentLevel=current.contains(item.getMenuId())?"1":"0";
            String nextLevel=next.contains(item.getMenuId())?"1":"0";
            DepartmentRoleMenu departmentRoleMenu = new DepartmentRoleMenu(item.getDepartmentRoleMenuId(), dto.getDepartmentRoleId(), item.getMenuId(), currentLevel, nextLevel);
            if(StringUtils.hasText(departmentRoleMenu.getDepartmentRoleMenuId())){
                departmentRoleMenuMapper.updateByPrimaryKeySelective(departmentRoleMenu);
            }else {
                departmentRoleMenuMapper.insertSelective(departmentRoleMenu);
            }
        }

    }

    @Override
    public void saveList(List<DepartmentRoleMenu> saveList) {
        departmentRoleMenuMapper.saveList(saveList);
    }


}
